package com.gla.market.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.PopupWindow;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gla.market.R;
import com.gla.market.activity.HistoryOrderActivity;
import com.gla.market.constant.Constants;
import com.gla.market.entity.Order;
import com.gla.market.entity.OrderDetail;
import com.gla.market.entity.OrderDetailResponse;
import com.gla.market.entity.OrderResponse;
import com.gla.market.entity.ShopInCart;
import com.gla.market.utils.NetUtil;

import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * 订单里的商店适配器
 */
public class HistoryOrderShopAdapter extends RecyclerView.Adapter<HistoryOrderShopAdapter.ViewHolder> {

    private List<OrderResponse> orderData;

    private Context context;

    public HistoryOrderShopAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<OrderResponse> orderData) {
        this.orderData = orderData;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_history_order_shop, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        int pos = position;
        OrderResponse ordersp = orderData.get(position);
        Order order = ordersp.getOrder();
        List<OrderDetailResponse> list = ordersp.getList();
        if(list.size()>0) {
            holder.tv_shopName.setText(list.get(0).getCommodity().getShop().getName());
        }
        holder.tv_remarks.setText(StringUtils.isBlank(order.getRemarks()) ? "无备注":order.getRemarks());

        holder.bt_evaluate.setVisibility(View.GONE);
        holder.bt_delete.setVisibility(View.GONE);
        holder.bt_chargeback.setVisibility(View.GONE);
        holder.bt_receipt.setVisibility(View.GONE);
        holder.bt_pay.setVisibility(View.GONE);
        String status="订单状态";
        switch ((int)order.getState()){
            case Order.PLACED:
                status = "未支付";
                holder.bt_pay.setVisibility(View.VISIBLE);
                holder.bt_delete.setVisibility(View.VISIBLE);//显示删除按钮
                break;
            case Order.PAID:
                status = "已支付";
                holder.bt_chargeback.setVisibility(View.VISIBLE);//显示退单按钮
                break;
            case Order.SHIPPED:
                status = "已发货";
                holder.bt_chargeback.setVisibility(View.VISIBLE);//显示退单按钮
                holder.bt_receipt.setVisibility(View.VISIBLE);
                break;
            case Order.RECEIVED:
                status = "已收货";
                holder.bt_evaluate.setVisibility(View.VISIBLE);//显示评价按钮
                holder.bt_delete.setVisibility(View.VISIBLE);//显示删除按钮
                break;
            case Order.EVALUATED:
                holder.bt_delete.setVisibility(View.VISIBLE);//显示评价按钮
                status = "已评价";
                break;
            case Order.CANCELED:
                status = "已取消";
                holder.bt_delete.setVisibility(View.VISIBLE);//显示删除按钮
                break;
            case Order.CHARGEBACK:
                status = "申请退单中";
                break;
            case Order.REFUNDED:
                status = "已退单";
                holder.bt_delete.setVisibility(View.VISIBLE);//显示删除按钮
                break;
        }
        holder.tv_status.setText(status);

        //收货按钮
        holder.bt_receipt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent("com.gla.market.RECEIPT_ORDER");
                intent.putExtra("order_id",order.getId());
                context.sendBroadcast(intent);
            }
        });
        //支付按钮
        holder.bt_pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent("com.gla.market.PAY_ORDER");
                intent.putExtra("order_id",order.getId());
                context.sendBroadcast(intent);
            }
        });
        //退单按钮
        holder.bt_chargeback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent("com.gla.market.CHARGE_BACK_ORDER");
                intent.putExtra("order_id",order.getId());
                context.sendBroadcast(intent);
            }
        });
        //评价按钮
        holder.bt_evaluate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //弹评价窗
                View contentView = LayoutInflater.from(context).inflate(R.layout.order_evaluation_pop_window, null);
                PopupWindow popWindow = new PopupWindow (contentView, ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
                TextView input = contentView.findViewById(R.id.eva_input);
                RatingBar ratingBar = contentView.findViewById(R.id.eva_rating);
                //设置可获取焦点
                popWindow.setFocusable(true);
                //设置可点击弹窗外内容
                popWindow.setBackgroundDrawable(new BitmapDrawable());
                popWindow.setOutsideTouchable(true);
                //设置关闭处关闭弹窗
                contentView.findViewById(R.id.eva_close).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        popWindow.dismiss();
                    }
                });

                //提交按钮
                Button bt_submit = contentView.findViewById(R.id.eva_submit);
                bt_submit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent("com.gla.market.EVALUATE_ORDER");
                        intent.putExtra("order_id",order.getId());
                        intent.putExtra("evaluation",input.getText().toString());
                        intent.putExtra("point",(int)ratingBar.getRating());
                        context.sendBroadcast(intent);
                        popWindow.dismiss();
                    }
                });
                popWindow.showAtLocation(holder.tv_shopName, Gravity.BOTTOM, 0,0);
            }
        });
        //删除按钮
        holder.bt_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent("com.gla.market.DELETE_ORDER");
                intent.putExtra("order_id",order.getId());
                context.sendBroadcast(intent);
            }
        });
        HistoryOrderItemAdapter adapter = new HistoryOrderItemAdapter(context,list);
        holder.rv_cartList.setLayoutManager(new LinearLayoutManager(context));
        holder.rv_cartList.setAdapter(adapter);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return orderData==null?0:orderData.size();
    }

    //初始化控件
    class ViewHolder extends RecyclerView.ViewHolder {

        TextView tv_shopName;
        RecyclerView rv_cartList;
        TextView tv_remarks;
        TextView tv_status;
        Button bt_evaluate;
        Button bt_delete;
        Button bt_chargeback;
        Button bt_pay;
        Button bt_receipt;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_shopName = itemView.findViewById(R.id.item_order_shop_name);
            rv_cartList = itemView.findViewById(R.id.order_cart_list);
            tv_remarks = itemView.findViewById(R.id.order_remarks);
            tv_status = itemView.findViewById(R.id.order_status);
            bt_evaluate = itemView.findViewById(R.id.item_order_evaluate);
            bt_delete = itemView.findViewById(R.id.item_order_delete);
            bt_chargeback = itemView.findViewById(R.id.item_order_chargeback);
            bt_pay = itemView.findViewById(R.id.item_order_pay);
            bt_receipt = itemView.findViewById(R.id.item_order_receipt);
        }
    }
}
